package com.hex.ds.hdtp.core.inf.data.contrast.service.impl;

import com.hex.ds.hdtp.core.inf.common.utils.JdbcConnectUtil;
import com.hex.ds.hdtp.core.inf.metadata.entity.TableFieldsEntity;
import com.hex.ds.hdtp.core.inf.metadata.po.SourceConfInfoPo;
import com.hex.ds.hdtp.core.inf.metadata.repository.ISourceConfInfoRepository;
import com.hex.ds.hdtp.core.inf.metadata.service.impl.DmMethodService;
import com.hex.ds.hdtp.core.inf.metadata.service.impl.SourceMethodHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.List;

/**
 * @Package com.hex.ds.hdtp.core.inf.data.contrast.service.impl
 * @ClassName DmInfoService
 * @Description Dm数据库操作
 * @Author Yin.Yang
 * @Date 2023/10/31 18:02
 * @Version v1.0
 **/
@Slf4j
@Service("COMPARE_DM")
public class DmInfoService extends AbstractDataInfoService {

    private final DmMethodService dmMethodService;

    public DmInfoService(ISourceConfInfoRepository sourceConfInfoRepository, SourceMethodHandlerContext sourceMethodHandlerContext, DmMethodService dmMethodService) {
        super(sourceConfInfoRepository, sourceMethodHandlerContext);
        this.dmMethodService = dmMethodService;
    }

    /**
     * @param sourceCode
     * @param schema
     * @param table
     * @Method selectTableExist
     * @Return java.lang.Boolean
     * @Description 是否有表
     * @Author Yin.Yang
     * @Date 2023/10/31 17:52
     * @Version V1.0
     */
    @Override
    public Boolean selectTableExist(String sourceCode, String schema, String table) throws SQLException {
        List<SourceConfInfoPo> sourceConfInfoPoList = getSourceConfInfoRepository().queryListBySourceCode(sourceCode);
        String querySql = dmMethodService.queryTableExistSql(schema, table);
        Long tableCount = JdbcConnectUtil.getLongResult(sourceConfInfoPoList, querySql);
        return tableCount == 1L ? Boolean.TRUE : Boolean.FALSE;
    }

    /**
     * @param sourceCode
     * @param schema
     * @param table
     * @Method selectAllFieldNum
     * @Return java.lang.Long
     * @Description 字段总数
     * @Author Yin.Yang
     * @Date 2023/10/31 17:52
     * @Version V1.0
     */
    @Override
    public Long selectAllFieldNum(String sourceCode, String schema, String table) throws SQLException {
        List<SourceConfInfoPo> sourceConfInfoPoList = getSourceConfInfoRepository().queryListBySourceCode(sourceCode);
        String querySql = dmMethodService.getTableAllFieldNumSql(schema, table);
        return JdbcConnectUtil.getLongResult(sourceConfInfoPoList, querySql);
    }


    /**
     * @param tableFieldsEntities
     * @param partitionFieldName
     * @param isCover
     * @Method buildCreateTableSql
     * @Return java.lang.String
     * @Description 建立建表语句
     * @Author Yin.Yang
     * @Date 2023/10/31 17:50
     * @Version V1.0
     */
    @Override
    public String buildCreateTableSql(List<TableFieldsEntity> tableFieldsEntities, List<String> partitionFieldName, String isCover) {
        // 建表SQL头
        String createSqlHeader = dmMethodService.getCreateTableHeaderSql(tableFieldsEntities.get(0).getDataBaseName(), tableFieldsEntities.get(0).getTableName());
        // 建表SQL体--字段信息
        StringBuilder createSqlBody = new StringBuilder();
        for (TableFieldsEntity tableFields : tableFieldsEntities) {
            createSqlBody.append(dmMethodService.getCreateTableBodySql(tableFields.getColumnName(), tableFields.getColumnType(), tableFields.getIsPrimaryKey().equals("Y") ? "NOT NULL PRIMARY KEY" : ""));
        }
        // 建表SQL--尾括号
        String createSqlFooter = String.format(") \n");
        // 建表SQL--分区字段
        String createSqlPartition = "";
        for (TableFieldsEntity tableFields : tableFieldsEntities) {
            if (tableFields.getPartitioningColumn().equals("Y")) {
                createSqlPartition = dmMethodService.getCreateTablePartitionSql(tableFields.getColumnName());
            }
        }
        return createSqlHeader + createSqlBody.toString().substring(0, createSqlBody.lastIndexOf(",")).trim() + createSqlFooter + createSqlPartition;
    }


    @Override
    protected String selectDataNumSql(String schema, String table, String whereSql) {
        return dmMethodService.getTableDataNumSql(schema, table, whereSql);
    }

    @Override
    protected String selectCountSumIsNullSql(String schema, String table, String field, String whereSql) {
        if (whereSql != null && whereSql.trim().toUpperCase().startsWith("WHERE")) {
            whereSql = "AND " + whereSql.trim().substring(5);
        }
        return dmMethodService.getTableIsNullCountSql(schema, table, field, whereSql);
    }
}
